package util.validate
{
	import MyApp.Model.Business.Staff;
	
	import mx.collections.ArrayCollection;
	import mx.utils.StringUtil;
	import mx.validators.ValidationResult;
	import mx.validators.Validator;
	
	import nz.co.codec.flexorm.EntityManager;
	import nz.co.codec.flexorm.criteria.Criteria;
	
	import util.Constants;
	
	public class LoginUserValidator extends Validator
	{
		private var _userError:String;
		private var _adminError:String;
		[Bindable]
		private var _staff:Object;
		
		
		public function LoginUserValidator()
		{
			super();
		}

		public function get adminError():String
		{
			return _adminError;
		}

		public function set adminError(value:String):void
		{
			_adminError = value;
		}

		public function set staff(value:Object):void{
			this._staff=value;
		}
		[Bindable]
		public function get staff():Object{
			return this._staff;
		}
		public function set userError(value:String):void{
			this._userError=value;
		}
		public function get userError():String{
			return this._userError;
		}
		override protected function doValidation(value:Object):Array{
			var results:Array=super.doValidation(value);
			if(results.length>0){
				return results;
			}
			var username:String=String(value);
			username=StringUtil.trim(username);
			
			
			//数据库验证
			var em:EntityManager=EntityManager.getInstance();
			var cri:Criteria=em.createCriteria(Staff);
			cri.addEqualsCondition("nickName",username);
			var data:ArrayCollection=em.fetchCriteria(cri);
			if(data.length==0){
				if(username==Constants.ADMINISTRATORS) {
					results.push(new ValidationResult(true,"","adminError",adminError));
				}else {
					results.push(new ValidationResult(true,"","userError",userError));
				}
				
			}else{
				staff=data.getItemAt(0);
			}
			return results;
		}
	}
}